<?php
namespace Admin\Model;
use Think\Model;
class AdminModel extends Model 
{
	protected $insertFields = 'username,password,rpassword,status,addtime,checkcode';
	protected $updateFielsd = 'id,username,password,rpassword,status,addtime,role_id';
	protected $_validate = array(
		array('username', 'require', '用户名不能为空！', 1, 'regex', 3),
		array('password', 'require', '密码不能为空！', 1, 'regex', 1),
		array('password', 'rpassword', '两次密码不一致！', 1, 'confirm', 3),
	);
	//登录的验证规则
	public  $_login_validate = array(
			array('username','require','请填写用户名',1,'regex',1),
			array('password','require','请填写密码',1,'regex',1),
			array('checkcode','require','验证码不能为空',1,'regex',3),
			array('checkcode','chk_code','验证码不能为空',1,'callback',3),
			 
	);
	//验证码的判断
	public function chk_code($code){
		$verify = new \Think\Verify();
		return $verify ->check($code);
		
	}
	//登陆的逻辑代码
	public function login(){
		$username = I('post.username');
		$password = I('post.password');
		//先判断有没有这个账号
		$user = $this->where(array(
			'username'=>array('eq',$username),
		))->find();
		//var_dump($user);die();
		if($user){
			if($user['status'] == '正常'){
				if($user['password'] == md5($password)){
					session('id',$user['id']);
					session('username',$user['username']);
					return  true;
				}else{
					$this->error='密码不正确';
					return false;
				}
			}else{
				$this->error='账号被禁用';
				return false;
			}
		}else{
			$this->error='账号不存在';
			return false;
		}
		
	}
	//验证码的验证
	
	//使用钩子函数
	protected function _after_insert($data,$option){
		$role_id = I('post.role_id');
		//var_dump(I('post.role_id'));die();
	
		$a_r_Model = D('admin_role');
		if($role_id){
			foreach ($role_id as $k=>$v){
				$a_r_Model ->add(array(
					'admin_id'=>$data['id'],
					'role_id'=>$v,
				));
			}
		}
	}
	//添加时间
	
	protected function _before_insert(&$data,$option){
	
		//echo 'aaaa';die();
		$data['addtime'] = time();
		$data['password'] =md5($data['password']);
		$data['rpassword'] =md5($data['rpassword']);
	}
	//修改前
	protected function _before_update(&$data,$option){
		//先删除原来的数据
		$id = I('get.id');
		$a_r_model = D('admin_role');
		$a_r_model->where(array(
			'admin_id' => array('eq',$id),
		))->delete();
		//在添加
		$roleId = I('post.role_id');
		if($roleId){
			foreach($roleId as $v){
				$a_r_model->add(array(
					'admin_id'=>$id,
					'role_id'=>$v,
				));
			}
		}
		
	}
	//删除之前的钩子函数
	protected function _before_delete(&$data,$option){
		$id = I('get.id');
		if($id == 29){
			$this->error = "不允许删除超级管理员";
			return false;
		}
	}


	public function search($perPage = 5)
	{	
		$where = array();
		$adminModel = D('Admin');
		$a_r_model = D('admin_role');

		// var_dump($_GET);
		$rn = I('get.gn');
		if($rn){
			$where['a.username'] = array('like' ,"%$rn%");
		}

		/*************** 翻页 ****************/
		// 取出总的记录数
		$count = $this->where($where)->count();

		/*$count =  $adminModel->alias('a')->field('a.*,GROUP_CONCAT(c.role_name) role_name')
		->join('left join s50_admin_role b on a.id=b.admin_id left join s50_role c on b.role_id=c.id')
		->group('a.id')
		->where($where)
		->count()*/;


		// var_dump($count);

		// 生成翻页类的对象
		$pageObj = new \Think\Page($count, $perPage);
		// 设置样式
		
		$pageObj->setConfig('next', '下一页');
		$pageObj->setConfig('prev', '上一页');
		$pageObj->setConfig('first', '首页');
		$pageObj->setConfig('last', '尾页');
		// 生成页面下面显示的上一页、下一页的字符串
		$show = $pageObj->show();
		
	
		/************** 取某一页的数据 ***************/
		// $list = $this->where($where)->limit($pageObj->firstRow.','.$pageObj->listRows)->select();
		 //取出角色数据
     	$list =  $adminModel->alias('a')->field('a.*,GROUP_CONCAT(c.role_name) role_name')
		         ->join('left join s50_admin_role b on a.id=b.admin_id left join s50_role c on b.role_id=c.id')
		         ->group('a.id')
		         ->where($where)
		         ->limit($pageObj->firstRow.','.$pageObj->listRows)
		         ->select();
		// var_dump($list);
		
		/************** 返回数据 ******************/
		return array(
			'list' => $list,  // 数据
			'show' => $show,  // 翻页字符串
			'count'=> $count,//总记录数
		);
	}	
}
